package com.xiaotu.service;

import com.xiaotu.dao.FileDao;
import com.xiaotu.model.EnterpriseFileModel;
import com.xiaotu.model.ParamModel;
import com.xiaotu.utils.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @作者 王艳龙
 * @日期 2021-01-29 14:50
 * @功能
 */
@Service
@Transactional
public class FileService {

    @Autowired
    private FileDao fileDao;

    /**
     * 获取文件详细信息
     * @param param
     * @return
     */
    public List<EnterpriseFileModel> queryFileDetailInfo(ParamModel param){
        return fileDao.queryFileDetailInfo(param);
    }

    /**
     * 新增上传文件数据
     * @param fileModel
     * @throws Exception
     */
    public void addFileInfo(EnterpriseFileModel fileModel) throws Exception {
        fileDao.addFileInfo(fileModel);
    }

    /**
     * 根据id删除文件
     * @param baseDownLoadPath
     * @param fileId
     */
    public void deleteFileInfo(String baseDownLoadPath,Integer fileId){
        EnterpriseFileModel  fileModel = fileDao.queryFileInfo(fileId);
        if(null !=fileModel){
            //删除服务器上原文件
            if (StringUtils.isNotBlank(fileModel.getStorePath())) {
                FileUtils.deleteFile(baseDownLoadPath + fileModel.getStorePath());
            }
            //删除服务器上预览文件
            /*if (StringUtils.isNotBlank(fileModel.getPreviewStorePath())) {
                FileUtils.deleteFile(baseDownLoadPath + fileModel.getPreviewStorePath());
            }*/
            //删除数据库文件信息
            fileDao.deleteFileInfo(fileId);
        }

    }

    /**
     * 根据id获取文件信息
     * @param fileId
     * @return
     */
    public EnterpriseFileModel queryFileInfo(Integer fileId){
        return fileDao.queryFileInfo(fileId);
    }

    /**
     * 根据id获得企业用户基本信息
     * @param enterpriseId
     * @return
     */
    public Map<String,Object> queryEnterpriseInfo(String enterpriseId){
        return fileDao.queryEnterpriseInfo(enterpriseId);
    }

    /**
     * 修改企业基本信息营业照片
     * @param businessLicensePath
     * @param enterpriseId
     * @param fileName
     */
    public void updateEnterpriseInfo(String businessLicensePath,String fileName,String enterpriseId){
        fileDao.updateEnterpriseInfo(businessLicensePath,fileName,enterpriseId);
    }


}
